/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.log4j.Logger;

import com.dnc.cloak.framework.context.Context;
import com.dnc.cloak.framework.context.ContextImpl;
/**
 * Invokes local service calls
 */
public class ServiceLocalInvocationHandler<ServiceLocalInvocationHandlerType> implements InvocationHandler {
	
	private static final Logger logger = Logger.getLogger(ServiceLocalInvocationHandler.class.getName());
	private ServiceLocalInvocationHandlerType proxiedObject = null;

	public ServiceLocalInvocationHandler() {
		super();		
	}

    public ServiceLocalInvocationHandlerType getProxiedObject() {
        return proxiedObject;
    }

    public  void setProxiedObject(ServiceLocalInvocationHandlerType proxiedObject) {
        this.proxiedObject = proxiedObject;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        
        Class<?>[] methodArgClasses = method.getParameterTypes();
        
        logger.debug("ServiceLocalInvocationHandler.invoke");

        for (int i = 0; i < methodArgClasses.length; i++) {
        	
            logger.debug("arg classes:");
    		logger.debug("\t" + i + "=" + methodArgClasses[i]);
            
        	if (methodArgClasses[i] == Context.class) {        		
        		if (args[i] == null) {
	        		Context context = new ContextImpl();
	        		//SecurityFactory sf = SecurityFactory.getInstance();
	        		
	        		//CloakPrincipal principle = sf.createPrincipal("cloak-system", "unknown");
	        		//ISessionContext sessionContext = sf.createStatelessSession(principle);
	        		//ISecurityContext securityContext = sf.createSecurityContext(principle);
	        		
	        		//context.setSecurityContext(securityContext);
	        		//context.setSessionContext(sessionContext);
	        		
	        		args[i] = context;	
	        		
	        		logger.debug("created context:" + context.toString());
        		}
        	}
        	
        	//Class<?>[] interfaces = methodArgClasses[i].getInterfaces();
        	
        	//for (int j = 0; j < interfaces.length; j++) {
			//	interfaces[j].i
			//}
		}

    	if (logger.isDebugEnabled()){
            logger.debug("\tproxiedObject:\t" + proxiedObject.getClass().getPackage().getName() + "." + proxiedObject.getClass().getSimpleName());
            logger.debug("\tmethod:\t\t" + method.getName());
            logger.debug("\targs:");
            
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                if (arg != null){
                    logger.debug("\t\t" + arg.getClass().getName() + "=" + arg);
                }
                else {
                    logger.debug("\t\t" + "null");
                }
            }
        }

        
        long begin = new Date().getTime();
        Object returnValue = method.invoke(proxiedObject, args);
        long end = new Date().getTime();
       
        if (logger.isDebugEnabled()){
            logger.debug("\tmethodExecutionTime:\t" + (end - begin));
            logger.debug("\tresult:\t" + returnValue);
        }
        
        return returnValue;        
    }

}